/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package cn.edu.zut.jdbcpool.reflect;

import cn.edu.zut.jdbcpool.util.LogUtils;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.sql.Connection;
import java.util.List;

/**
 *
 * @author jyc
 */
public class ConnectionHandler implements InvocationHandler{
    private final Connection conn;
    private final List<Connection> list;

    public ConnectionHandler(Connection conn, List<Connection> list) {
        this.conn = conn;
        this.list = list;
    }
    
    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        if(!"close".equals(method.getName())) {
            return method.invoke(conn, args);
        } else {
            list.add(conn);
            LogUtils.info("push connection to pool, current count: " + list.size());
            return null;
        }
    }
    
}
